package com.eazytec.common.util.file.office;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import com.eazytec.common.util.file.properties.SystemConfig;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class DocumentHandler {

	private Configuration configuration = null;

	public DocumentHandler() throws IOException {
		configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
		// 设置模版装载方法和路径，FreeMarker支持多种模板装载方法。可以重servlet、classpath、数据库装载
		// 这里我们的模板是放在/resource/doc包下面，可以在erp.properties配置
		configuration.setClassicCompatible(true);
		configuration.setClassForTemplateLoading(this.getClass(),SystemConfig.getParam("erp.word.template.path"));
	}

	/**
	 * 
	 * @param fileName 生成的文件
	 * @param map 数据
	 * @param templet 要装载的模板
	 * @throws IOException
	 * @throws TemplateException
	 */
	public void exportWord(String fileName, Map<String, Object> map, String template) throws IOException, TemplateException {
		Template t = configuration.getTemplate(template);
		File outFile = new File(fileName);//输出文档路径及名称
		Writer out = null;
		out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
		t.process(map, out);
		out.close();
	}

}